#include <SDuFAT.h>
#define MEM_PW 8
int last_speed_value=0;

void EEPROM_writeUnsignedLong(int ee, unsigned long value)
{
    Serial.println("Write cyle");

    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++){
	  EEPROM.write(ee++, *p++);
//          Serial.println(ee);
    };
}

unsigned long EEPROM_readUnsignedLong(int ee)
{
    unsigned long value = 0;
    byte* p = (byte*)(void*)&value;
    for (int i = 0; i < sizeof(value); i++)
	  *p++ = EEPROM.read(ee++);
    return value;
}

// in diese routine werden die km hochgezählt und eventuell auf die karte/eeprom gespeichert
void km_save(){
  int speed_value=speed_copy();
  if(speed_value>0){
    storage_outdated=true;
    unsigned int strecke_m=round(speed_value/3.6*1);
    for(int a=0;a<=8;a++){
      trip_dist[a]+=strecke_m;
      avg_timebase[a]++;
      avg_speed[a]+=speed_value;
      
      if((last_speed_value/speed_value)>0.95 && (last_speed_value/speed_value)<1.05){
        if(speed_value>max_speed[a]){     
          max_speed[a]=speed_value;
        };
      };
    };
    last_speed_value=speed_value;
  } else { // laufender motor => sprit verbrauch => speichern
    dz_copy(); 
    if(dz_out_disp>0){
      storage_outdated=true;
    };
  };
  
  if(ss==59){
   if(storage_outdated){ // jede Minute
    Serial.println("=== Speichere Daten ===");
    // Write km to eeprom
    
    int adresse=6; //6 bis 6+8*4=38
    for(int i=1;i<9;i++){
      EEPROM_writeUnsignedLong(adresse,trip_dist[i]);
      adresse+=4;
    };
    
    // Write avg to eeprom
    adresse=38;//38 bis 38+8*7=38+56=102
    for(int i=1;i<9;i++){
      EEPROM_writeUnsignedLong(adresse,avg_speed[i]);
      adresse+=4;
      EEPROM_writeUnsignedLong(adresse,avg_timebase[i]);
      adresse+=4;
    };
    
    // Write max to eeprom
    adresse=102; //102 bis 134
    for(int i=1;i<9;i++){
      EEPROM_writeUnsignedLong(adresse,max_speed[i]);
      adresse+=4;
    };
       
    // write fuel to eeprom
    adresse=150; 
    int fuel[3];
    fuel_save(fuel);
    byte tempByte = (fuel[0] & 0xFF);
    if(fuel[0]>0){ // max has changed
      EEPROM.write(adresse,tempByte); // 150=max
    };
    adresse++;
    tempByte = (fuel[1] & 0xFF);
    EEPROM.write(adresse,tempByte); // 151=ist stand
    adresse++;
    tempByte = (fuel[2] & 0xFF);
    EEPROM.write(adresse,tempByte); // 152=ticker stand
    
    // write "today"
    tempByte = ((int)floor(clock_getdate()/100) & 0xFF);
    EEPROM.write(4,tempByte);
    tempByte = ((int)floor(clock_getdate()%100) & 0xFF);
    EEPROM.write(3,tempByte);
    
    storage_outdated=false;
    Serial.println("done");
   };
  };
};

void store_init(){
  Serial.println("-> Lade KM von SD Karte");
  pinMode(MEM_PW, OUTPUT);
  digitalWrite(MEM_PW, HIGH);
  
  flasher_1_mode= EEPROM.read(0); 
  flasher_2_mode= EEPROM.read(1); 
  trip_mode=EEPROM.read(2);
  
  disp_brightness=EEPROM.read(5);
  disp_contrast=EEPROM.read(149);
  analogWrite(disp_backlight_pin,round(disp_brightness*255/100));
  analogWrite(disp_contrast_pin,disp_contrast) ;
 
   // immer reseten
  trip_dist[0]=0;
  max_speed[0]=0;
  avg_speed[0]=0;
  avg_timebase[0]=0;
  
  int adresse=6;
  for( int i=1;i<9;i++){     
    trip_dist[i]=EEPROM_readUnsignedLong(adresse);
    adresse+=4;
  };
  
  adresse=102;
  for(int i=1;i<9;i++){
    max_speed[i]=EEPROM_readUnsignedLong(adresse);
    adresse+=4;
    if(storage_debug){
      Serial.print("Laut Speicher war der Max Nr ");
      Serial.print(i);
      Serial.print(" ");
      Serial.print(max_speed[i]);
      Serial.print("\n");
    };
  };
 
  adresse=38;
  for(int i=1;i<9;i++){
    avg_speed[i]=EEPROM_readUnsignedLong(adresse);
    adresse+=4;
    avg_timebase[i]=EEPROM_readUnsignedLong(adresse);
    adresse+=4;
  };

  //load date_of_today
  int temp =EEPROM.read(3);  
  int temp2=EEPROM.read(4); 
  int date_of_today=temp+100*temp2;
  
  // reset day trip at next day
  if(storage_debug){
    Serial.print("Laut speicher war das Datum zuletzt der ");
    Serial.print(date_of_today);
    Serial.print(" und heute ist der ");
    Serial.print(clock_getdate());
    Serial.print("\n");
  };
  if(clock_getdate()!=date_of_today){
    trip_dist[1]=0;
    max_speed[1]=0;
    avg_speed[1]=0;
    avg_timebase[1]=0;
  };
  
  // load fuel stage
  fuel_set(EEPROM.read(152),EEPROM.read(151),EEPROM.read(150));
  
  Serial.println("Done.");
};
